#!/usr/local/bin/python3
# *_* coding: UTF-8 *_*
# @IDE: PyCharm
# @Version: Python3.97
# @Author: Kendrick.Kc
# @Email: 509556985@qq.com
# @File: timestamp.py
# @Inst: 这里是模块名描述
# @Time: 2023/6/23 13:05
# -----


import time
import datetime


def now_time_stamp():
    """
    当前时间戳
    :return: 13位的时间戳
    """
    return int(time.time() * 1000)


def to_day_time_stamp():
    """
    根据当前的日期转换为时间戳
    :return: 13位的时间戳
    """
    to_date = time.strptime(str(datetime.date.today()), "%Y-%m-%d")  # 定义格式
    time_stamp = int(time.mktime(to_date) * 1000)
    return time_stamp


def time_stamp_date(time_stamp=None):
    """
    返回时间戳的日期
    :param time_stamp: 13位的时间戳
    :return: 日期
    """
    def res(ts):
        time_local = time.localtime(int(ts) / 1000)
        return time.strftime("%Y-%m-%d", time_local)

    if time_stamp:
        if len(str(time_stamp)) == 13:
            return res(time_stamp)
    else:
        return res(time.time() * 1000)


def next_time_stamp(binance, symbol, days=2):
    """ 获取第二天的时间戳(如果当前为0点之后8点之前, 那么则跳过今日8点，取明日8点(K线有滞后性, 保留当天或许可行)) """
    t = datetime.datetime.now() + datetime.timedelta(days=days)
    res_open_time_stamp = binance.rest("while_info", "klines", symbol=symbol, interval="1d", limit=1)[0][0]
    hour = time.localtime(res_open_time_stamp / 1000).tm_hour
    un_time = time.mktime(t.replace(t.year, t.month, t.day, hour, 1, 0, 0).timetuple())
    return int(un_time * 1000)
    # return int(time.time() * 1000) + 360000

